//
//  ZHLEENewFeatureController.swift
//  ZHLEEBlog_swift_pro
//
//  Created by ZHLEEBlander on 15/10/14.
//  Copyright © 2015年 ZHLEEBlander. All rights reserved.
//

import UIKit

class ZHLEENewFeatureController: UIViewController, UIScrollViewDelegate {
    
    var pageControl : UIPageControl?

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let scrollView = UIScrollView(frame: UIScreen.mainScreen().bounds)
        
        scrollView.delegate = self
        
        scrollView.backgroundColor = UIColor.whiteColor()
        
        scrollView.pagingEnabled = true
        
        scrollView.showsHorizontalScrollIndicator = false
        
        let count = 4
        for i in 0..<count {
            
            let imageView = UIImageView()
            
            imageView.image = UIImage(named: "new_feature_\(i+1)")
            
            imageView.size = scrollView.size
            
            imageView.x = CGFloat(i) * scrollView.width
            
            //添加子控件
            scrollView.addSubview(imageView)
            
            if i == count - 1 {
            
              setLastView(imageView)
            }
        }
        scrollView.contentSize = CGSizeMake(CGFloat(count) * scrollView.width, 0)
        
        view.addSubview(scrollView)
        
        let pageCon = UIPageControl()
        
        pageCon.numberOfPages = count
        pageCon.centerX = scrollView.width * 0.5
        pageCon.y = ScreenH - 60
        
        pageCon.pageIndicatorTintColor = UIColor.blackColor()
        pageCon.currentPageIndicatorTintColor = UIColor.orangeColor()
        
        view.addSubview(pageCon)
        self.pageControl = pageCon
    }
    
    /**
    设置最后一张view的时候调用
    
    - parameter imageView: <#imageView description#>
    */
    private func setLastView(imageView : UIImageView){
        
        //开启用户交互
        imageView.userInteractionEnabled = true
        //进入按钮
        let enterBtn = UIButton.button(title: "进入微博", bgImage: "new_feature_finish_button", target: self, action: "LoginClick:")
        enterBtn.centerX = ScreenW * 0.5
        enterBtn.y = ScreenH - 200
        
        imageView.addSubview(enterBtn)
        
        //分享按钮
        let sharedBtn = UIButton.button(title: " 分享到微博", image: "new_feature_share_false",  selectedImage: "new_feature_share_true", target: self, action: "click:")
        sharedBtn.centerX = enterBtn.centerX
        sharedBtn.y = enterBtn.y - 50
        
        imageView.addSubview(sharedBtn)
        
        
    }
    
    @objc private func LoginClick(button:UIButton){
        
        let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
        let window = delegate.window!
        window.rootViewController = delegate.defaultViewController()
    }
    
    @objc private func click(button:UIButton){
    
        button.selected = !button.selected
    }
    
    // MARK: - UIScrollViewDelegate
    func scrollViewDidScroll(scrollView: UIScrollView) {
        let page = scrollView.contentOffset.x / scrollView.width
        
        self.pageControl?.currentPage = Int(page + 0.5)
    }

}
